"""
Case Type   : tinyblob
Case Name   : tinyblob类型不支持创建MOT表
Create At   : 2022/03/21
Owner       : @li-xin12345
Description :
    1. 创建兼容B库，检查dolphin插件
    2. 设置enable_incremental_checkpoint=off
    3. 创建mot表含tinyblob类型
    4. 恢复环境
Expect      :
    1. 创建成功，插件自动加载
    2. 设置成功
    3. 创建失败，合理报错
    4. 恢复成功
History     :
    modified by lvlintao at 2025/3/24，优化用例，适配MOT开关
"""

import os
import unittest

from yat.test import Node
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common


class MotSupportTinyblob(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.user = Node('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.commonsh = CommonSH('PrimaryDbUser')
        status = self.commonsh.get_db_cluster_status("detail")
        self.log.info(status)
        self.assertTrue("Normal" in status or "Degraded" in status)
        self.db_name = 'db_tinyblob_024'
        self.tb_name = 't_tinyblob_024'
        self.error_mes = 'Column type tinyblob is not supported'
        self.default_vales = self.common.show_param(
            'enable_incremental_checkpoint')
        self.mot_engine = "enable_mot_server"
        text = '--------开启mot特性  ' \
               'expect:修改成功，重启数据库成功--------'
        self.log.info(text)
        self.engine_res = self.common.show_param(f'{self.mot_engine}')
        self.commonsh.execute_gsguc('set',
                                    self.constant.GSGUC_SUCCESS_MSG,
                                    f'{self.mot_engine} = on')
        self.commonsh.restart_db_cluster()
        status = self.commonsh.get_db_cluster_status()
        self.log.info(status)
        self.assertTrue("Degraded" in status or "Normal" in status,
                        '执行失败:' + text)

    def test_tinyblob_mot(self):
        text = '-----step1:创建兼容B库，检查dolphin插件;expect:创建成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} " \
                  f"dbcompatibility ='B';"
        self.log.info(sql_cmd)
        sql_res = self.commonsh.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)

        sql_cmd = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd)
        sql_res = self.commonsh.execut_db_sql(sql_cmd,
                                              dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn('dolphin', sql_res, '查询失败')

        text = '-----step2:设置enable_incremental_checkpoint=off;' \
               'expect:成功-----'
        self.log.info(text)
        if 'off' not in self.default_vales:
            set_off = self.commonsh.execute_gsguc(
                "set", self.constant.GSGUC_SUCCESS_MSG,
                f'enable_incremental_checkpoint=off')
            self.log.info(set_off)
            status = self.commonsh.restart_db_cluster()
            self.log.info(status)
            res = self.common.show_param('enable_incremental_checkpoint')
            self.log.info(res)
            self.assertIn("off", res, "执行失败：" + text)

        text = '-----step3:创建mot表含tinyblob类型;expect:失败-----'
        self.log.info(text)
        sql_cmd = f'''
        drop foreign table if exists {self.tb_name};
        create foreign table {self.tb_name}(field_name tinyblob);
        '''
        res = self.commonsh.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(res)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, res,
                      "执行失败：" + text)
        self.assertIn(self.error_mes, res, "执行失败：" + text)

    def tearDown(self):
        text = '-----step4:恢复环境;expect:成功-----'
        self.log.info(text)
        drop_res = self.commonsh.execut_db_sql(
            f"drop database if exists {self.db_name};checkpoint;", dbname='postgres')
        self.log.info(drop_res)
        restores_default_value = self.commonsh.execute_gsguc(
            "set", self.constant.GSGUC_SUCCESS_MSG,
            f"enable_incremental_checkpoint={self.default_vales}")
        self.log.info(restores_default_value)
        text = '--------还原mot特性  ' \
               'expect:修改成功，重启数据库成功--------'
        self.log.info(text)
        self.commonsh.execute_gsguc('set',
                                    self.constant.GSGUC_SUCCESS_MSG,
                                    f'{self.mot_engine} = {self.engine_res}')
        restart_res = self.commonsh.restart_db_cluster()
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res,
                      "执行失败：" + text)
        self.assertTrue(restores_default_value, "执行失败:" + text)
        self.assertTrue(restart_res, '执行失败:' + text)
        self.log.info(f"-----{os.path.basename(__file__)} end-----")